package com.tracking.locon;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class TrackCircleOverlay extends Overlay {

    private Context mContext;
    private GeoPoint mGeoPoint;
    private float mMeter;
    
    private final int mMaxZoom = 18;
    private final int mMinZoom = 4;
    
    private Paint paintBorder, paintFill;
    
    public TrackCircleOverlay(Context context, GeoPoint geoPoint, float meter){
    	mContext = context;
    	mGeoPoint = geoPoint;
    	mMeter = meter;
    	//Utility.makeToast(g_context, "Draw circle");
    	setPaint();
    }
    
    /*
     * Set the color and effect on the circle
     */
    private void setPaint(){
    	paintBorder = new Paint();
    	paintBorder.setColor(Color.parseColor("#0099CC"));
    	paintBorder.setStrokeWidth(2);
    	paintBorder.setStrokeCap(Paint.Cap.ROUND);
        paintBorder.setAntiAlias(true);
        paintBorder.setDither(false);
        paintBorder.setStyle(Paint.Style.STROKE);
        
        paintFill = new Paint();
        paintFill.setColor(Color.parseColor("#33B5E5"));
        paintFill.setStyle(Paint.Style.FILL);
        paintFill.setAlpha(25);
    }
    
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow){
    	super.draw(canvas, mapView, shadow);
    	
    	Point point = new Point();
    	Projection projection = mapView.getProjection();
    	projection.toPixels(mGeoPoint, point);

    	float radius = Utility.metersToRadius(mMeter, mapView, mGeoPoint.getLatitudeE6() /1e6);
    	
    	canvas.drawCircle((float)point.x, (float)point.y, radius, paintFill);
    	canvas.drawCircle((float)point.x, (float)point.y, radius, paintBorder);
    	
    	setZoomLevelLimit(mapView);
    }
    
    /*
     * Set the maximum and minimum zoom level
     */
    public void setZoomLevelLimit(MapView mapView){
    	if (mapView.getZoomLevel() < mMinZoom)
            mapView.getController().setZoom(mMinZoom);
    	if (mapView.getZoomLevel() > mMaxZoom)
    		mapView.getController().setZoom(mMaxZoom);
    }
}
